<template>
  <div class="app-container">
    <el-form :inline="true" ref="ruleForm" :model="formInline" class="demo-form-inline">
      <el-form-item label="申请调整时间" prop="time">
        <el-date-picker
          v-model="formInline.time"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          :default-time="['00:00:00', '23:59:59']"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerDayStep">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="调整金额">
        <el-col :span="11">
          <el-form-item prop="minAmount">
            <el-input @keyup.enter.native="$emit('on-filter',formInline)" v-model="formInline.minAmount" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="maxAmount">
            <el-input @keyup.enter.native="$emit('on-filter',formInline)" v-model="formInline.maxAmount" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item prop="userType">
       <el-select v-model="formInline.userType">
          <el-option
            v-for="item in consts.ACCOUNT_TYPE"
            :label="item.title"
            :key="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="keywords" class="with-pre-select">
        <el-input @keyup.enter.native="$emit('on-filter',formInline)" v-model="formInline.keywords" placeholder="请输入账户名">
          <!-- <template slot="prepend">
            <el-select v-model="formInline.userType">
              <el-option
                v-for="item in consts.ACCOUNT_TYPE"
                :label="item.title"
                :key="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </template> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="changeType" label="调整类型">
        <el-select v-model="formInline.changeType" @change="handlechange">
          <!--<el-option label="全部" value=""></el-option>-->
          <el-option
            v-for="item in consts.ADJUSTMENT_TYPES_DETAIL"
            :label="item.label"
            :key="item.value"
            :value="item.value"
            v-if="dataRoleCheck(dataPermiAdjustTypeObj[item.value])"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="subType" label="细分类型" :disabled="index==undefined">
        <el-select v-model="formInline.subType">
          <el-option label="全部" :value="this.subTypePermissionValues.join(',')"></el-option>
          <template v-if="index!==undefined">
            <el-option
              v-for="item in consts.ADJUSTMENT_TYPES_DETAIL[index].children"
              :label="item.label"
              :key="item.value"
              :value="item.value"
              v-if="(item.value===9?(index===1?dataRoleCheck(dataPermiAdjustTypeDetailObj[item.value][0]):dataRoleCheck(dataPermiAdjustTypeDetailObj[item.value][1])):dataRoleCheck(dataPermiAdjustTypeDetailObj[item.value]))"
            ></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="creator">
        <el-select v-model="formInline.creator" placeholder="请选择申请人">
          <el-option
            v-for="(item,index) in auditApplyList"
            :label="item"
            :key="index"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="一审人" prop="firstApprover">
        <el-select v-model="formInline.firstApprover" placeholder="请选择一审人">
          <el-option
            v-for="item in auditFirList"
            v-if="item"
            :label="item"
            :key="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二审人" prop="secondApprover">
        <el-select v-model="formInline.secondApprover" placeholder="请选择二审人">
          <el-option
            v-for="item in auditSecList"
            v-if="item"
            :label="item"
            :key="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="formInline.status" placeholder="请选择审核状态">
          <el-option
            v-for="item in consts.AUDIT_STATUS"
            :label="item.title"
            :key="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="$emit('on-filter',formInline)">搜索
      </el-button>
      <el-button @click="selfResetForm('ruleForm')">重置</el-button>
    </el-form>
  </div>

</template>

<script>
import { getCreatorList, getFirstApproverList, getSecondApproverList } from '@/services/api/adjustment';
import { form, pickerOption } from '@/build';

export default {
  name: 'audit-filter',
  mixins: [form, pickerOption],
  data() {
    const fromDate = moment().format('YYYY-MM-DD 00:00:00');
    const endDate = moment().format('YYYY-MM-DD 23:59:59');
    return {
      index: 0,
      auditApplyList: [],
      auditFirList: [],
      stepFir: { step: 1 },
      stepSec: { step: 2 },
      auditSecList: [],
      formInline: {
        time: [
          fromDate,
          endDate,
        ],
        minAmount: undefined,
        maxAmount: undefined,
        userType: -1,
        keywords: undefined,
        creator: undefined,
        firstApprover: undefined,
        secondApprover: undefined,
        status: undefined,
        changeType: undefined,
        subType: undefined,
      },
      subTypePermissionValues: [],
      dataPermiAdjustTypeObj: {
        1009: 'addMoney-addToBonus',
        1018: 'addMoney-noAddToBonus',
        1011: 'minusMoney',
      },
      dataPermiAdjustTypeDetailObj: {
        1: 'redPacket',
        2: 'bonusActivity',
        3: 'manualRecharge',
        4: 'withdrawFailedSendBack',
        5: 'transferAddScore',
        6: 'gameAddScore-ballBetSport',
        7: 'gameAddScore-LbLottery',
        8: 'addScoreBonus',
        9: ['addMoneyCommissionAdjust', 'minusMoneyCommissionAdjust'],
        10: 'systemBrokerage',
        11: 'mistakeAddScoreSendBack',
        25: 'virtualLines',
      },
    };
  },
  watch: {
    'formInline.changeType': {
      handler(newVal) {
        if (newVal == 1009) { // 加币-计入红利
          this.index = 0;
        } else if (newVal == 1018) { // 加币-不计入红利
          this.index = 1;
        } else if (newVal == 1011) { // 减币
          this.index = 2;
        }
        // else {
        //   this.index = undefined;
        // }
        this.subTypePermissionValues = [];
        _.each(consts.ADJUSTMENT_TYPES_DETAIL[this.index].children, (item) => {
          if (item.value === 9) {
            if (this.dataRoleCheck(this.dataPermiAdjustTypeDetailObj[item.value][0]) || this.dataRoleCheck(this.dataPermiAdjustTypeDetailObj[item.value][1])) {
              this.subTypePermissionValues.push(item.value);
            }
          } else if (this.dataRoleCheck(this.dataPermiAdjustTypeDetailObj[item.value])) {
            this.subTypePermissionValues.push(item.value);
          }
        });
        this.formInline.subType = this.subTypePermissionValues.join(',');
      },
      deep: true,
    },
  },
  methods: {
    initChangeType() {
      if (this.dataRoleCheck('addMoney-addToBonus')) {
        this.formInline.changeType = 1009;
      } else if (!this.dataRoleCheck('addMoney-addToBonus') && this.dataRoleCheck('addMoney-noAddToBonus')) {
        this.formInline.changeType = 1018;
      } else if (!this.dataRoleCheck('addMoney-addToBonus') && !this.dataRoleCheck('addMoney-noAddToBonus') && this.dataRoleCheck('minusMoney')) {
        this.formInline.changeType = 1011;
      } else if (!this.dataRoleCheck('addMoney-addToBonus') && !this.dataRoleCheck('addMoney-noAddToBonus') && !this.dataRoleCheck('minusMoney')) {
        this.formInline.changeType = undefined;
      }
    },
    handlechange() {
      this.formInline.subType = undefined;
    },
    selfResetForm(formName) {
      this.resetForm(formName);
      this.initChangeType();
      this.$emit('on-filter', this.formInline);
      // this.$parent.getAll()
    },
  },
  mounted() {
    this.initChangeType();
    this.bindGet(getCreatorList, 'auditApplyList', {});
    this.bindGet(getFirstApproverList, 'auditFirList', { });
    this.bindGet(getSecondApproverList, 'auditSecList', { });
    this.getAll();
  },
};
</script>

<style lang="scss" scoped>
  .with-pre-select {
    .el-input-group__prepend {
      .el-select {
        width: 120px;
      }
    }
  }

  .money-range {
    .el-input {
      width: 100px;
    }
  }

</style>
